#include "numlockmanager.h"
#include "ui_numlockmanager.h"

NumLockManager::NumLockManager(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::NumLockManager)
{
    ui->setupUi(this);
    setWindowTitle("NumLockManager V1.3");
    setFixedSize(490, 170);
    setWindowIcon(tray_icon_on);
    setWindowFlags(Qt::WindowCloseButtonHint);

    QString texts[6] = {"автозапуск", "ввімк/Вимк", "лише ввімк", "лише вимк", "про програму" , "закрити програму"};

    for (int i = 0; i < 6; i++){
        actions[i]->setText(texts[i]);
        menu->addAction(actions[i]);
        if (i == 0 || i == 3) menu->addSeparator();
        if (i == 0) menu->addAction("  Режим");
    }

    action_autostart->setData(autostart->status());
    action_autostart->setIcon(autostart->status() ? icon_check_true : icon_check_false);

    set_mode(config.getMode());

    tray_icon->setIcon(tray_icon_off);
    tray_icon->setContextMenu(menu);
    tray_icon->show();

    ss->moveToThread(ss);
    ss->start();
    mode->moveToThread(mode);

    connect(ss, &StatusShow::status, this, &NumLockManager::getStatus);
    connect(tray_icon, &QSystemTrayIcon::activated, this, &NumLockManager::icon_activated);
    connect(action_exit, &QAction::triggered, this, &QApplication::quit);
    connect(menu, &QMenu::triggered, this, &NumLockManager::set_menu_hz);
}

void NumLockManager::getStatus(bool status)
{
    tray_icon->setIcon( status ? tray_icon_on : tray_icon_off );
    setWindowIcon( status ? tray_icon_on : tray_icon_off );
}

void NumLockManager::set_menu_hz(QAction *action)
{
    for (int i = 0; i < 3; i++)
        if (action == actions_mode[i]) set_mode(i);

    if (action == action_autostart){
        bool data = action_autostart->data().toBool();
        action_autostart->setIcon( data ? icon_check_false : icon_check_true);
        action_autostart->setData( !data );
        autostart->switch_();
//        data ? settings->remove(MY_APP_NAME) : settings->setValue(MY_APP_NAME, MY_APP_PATH);

    }

    if (action == action_about) showNormal();
}

void NumLockManager::set_mode(int index)
{
    mode->setMode(index);
    config.setMode(index);

    for (int i = 0; i < 3; i++)
        actions_mode[i]->setIcon((i == index) ? icon_radio_true : icon_radio_false);

    if (index > 0)
        mode->start();
}
